package tester;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import comunicacao.MessageRedirectorInterface;
import comunicacao.ReceiveMessageInterface;

/**
 * classe que simula um cliente da base de dados
 * 
 * @author Leonardo Lino Nº45340
 * @author Diogo Pires Nº40351
 */
public class TestClient {
	static public void main(String args[]) {
		ReceiveMessageInterface rmiServer;
		MessageRedirectorInterface primary;
		Registry registry;
		Registry registry2;
		String serverAddress = "127.0.0.1";
		String serverPort = "3333";

		System.out.println("sending request to " + serverAddress + ":"
				+ serverPort);
		try {
			registry = LocateRegistry.getRegistry(serverAddress, (new Integer(
					serverPort)).intValue());

			primary = (MessageRedirectorInterface) (registry.lookup("dbServer1"));
			
			String redirection = primary.getResponsible("teatro1");
			
			String[] parts = redirection.split(":");
			String address = parts[0];
			int port = Integer.parseInt(parts[1]);
			String bindName = parts[2];
			
			registry2 = LocateRegistry.getRegistry(address, port);
			rmiServer = (ReceiveMessageInterface) (registry2.lookup(bindName));
			
			//System.out.println("Reservation result: " + rmiServer.reserveSeat("teatro1", 3, 40351, "hoje", false));
			System.out.println("Reservation removal result: " + rmiServer.removeSeatReserve("teatro1", 3, 40351, false));
			System.out.println("Purchase result: " + rmiServer.purchaseSeat("teatro1", 3, 40352, false));

		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (NotBoundException e) {
			e.printStackTrace();
		}
	}
}
